﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Xss.Data.Common.SessionStore
{
    /// <summary>
    /// 会话存储
    /// </summary>
    public interface ISessionStore
    {
        /// <summary>
        /// 当前会话
        /// </summary>
        IAdoSession CurrentSession { get; }

        /// <summary>
        /// 存储会话
        /// </summary>
        /// <param name="session"></param>
        void Store(IAdoSession session);

        void Release();
    }

    /// <summary>
    /// 会话存储器抽象类
    /// </summary>
    public abstract class SessionStore : MarshalByRefObject, ISessionStore
    {
        const string KEY = "_XSS_ADO_SESSION_";

        protected string sessionName = string.Empty;

        protected SessionStore(string id)
        {
            sessionName = KEY + id;
        }

        /// <summary>
        /// 当前会话实例
        /// </summary>
        public abstract IAdoSession CurrentSession
        {
            get;
        }

        /// <summary>
        /// 存储会话
        /// </summary>
        /// <param name="session"></param>
        public abstract void Store(IAdoSession session);

        /// <summary>
        /// 释放
        /// </summary>
        public abstract void Release();

        /// <summary>
        /// 获取会话存储对象
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static ISessionStore GetSessionStore(string Id)
        {
            if (System.Web.HttpContext.Current == null)
            {
                return new CallContextSessionStore(Id);
            }
            return new WebSessionStore(Id);
        }
    }
}

